
capture log close
log using output//voter_analysis_log.txt, replace text

// A program for making a table of t-test results
capture program drop ttesttable
program ttesttable
	syntax varlist, byvar(string) filename(string) [order(real 1)]
	preserve
		capture drop __*
		quietly{
			gen __lab = ""
			gen __n1 = .
			gen __n2 = .
			gen __mu1 = .
			gen __mu2 = .
			gen __p = .
			gen __diff = .
		}
		local i = 1
		foreach v in `varlist' {
			noisily: disp ">>> `v'"
			ttest `v', by(commented)
			quietly{
				local vlab : var label `v'
				replace __lab = "`vlab'" in `i'
				replace __n1 = r(N_1) in `i'
				replace __n2 = r(N_2) in `i'
				replace __mu1 = r(mu_1) in `i'
				replace __mu2 = r(mu_2) in `i'
				replace __p = r(p) in `i'
			}
			local i = `i'+1
		}
		quietly{
			replace __diff = __mu2-__mu1
			tostring __mu1 __mu2 __diff, replace force format("%8.3f")
			replace __diff = __diff + "*" if __p<.05
			replace __diff = __diff + "*" if __p<.01
			format %12.0gc __n1 __n2
		}
		listtex __lab __n2 __mu2 __n1 __mu1 __diff using `filename' if __lab!="", replace end(" \\") delimiter(" & ")
		drop __*
	restore
end

// Table 2
use voters_for_analysis, clear
ttesttable age length female party_d party_r party_i pct_elections_voted, ///
	byvar(commented) filename(output//table2.tex)

// Table 3
use voters_for_analysis, clear
tab town
scalar total_towns = r(r)
logit commented age length female party_d party_i pct_elections_voted
outreg2 using output//table3.txt, ///
		label dec(3) 2aster nocons nor2 ///
		addstat(Towns, total_towns) ///
		ctitle("All Towns") replace

logit commented age length female party_d party_i pct_elections_voted ///
	town_avg_age town_avg_length pct_reg_D pct_reg_R town_avg_pct_elections_voted
outreg2 using output//table3.txt, ///
		label dec(3) 2aster nocons nor2 ///
		drop(town* pct_reg*) ///
		addstat(Towns, total_towns) ///
		addtext(Town Controls,  X) ctitle("All Towns") append

logit commented age length female party_d party_i pct_elections_voted i.town_id
outreg2 using output//table3.txt, ///
		label dec(3) 2aster nocons nor2 ///
		drop(i.town_id*) ///
		addstat(Towns, total_towns) ///
		addtext(Town FEs,  X) ctitle("All Towns") append tex(frag)
		

// Table 4
use comments_merged_anon, clear
logit support age length_of_reg female party_d party_i pct_elections_voted n_comments
outreg2 using output//table4.txt, ///
		label dec(3) 2aster nocons nor2 ///
		ctitle("DV=Support") replace
logit neutral age length_of_reg female party_d party_i pct_elections_voted n_comments
outreg2 using output//table4.txt, ///
		label dec(3) 2aster nocons nor2 ///
		ctitle("DV=Neutral") append
logit oppose age length_of_reg female party_d party_i pct_elections_voted n_comments
outreg2 using output//table4.txt, ///
		label dec(3) 2aster nocons nor2 ///
		ctitle("DV=Oppose") append
logit n_or_o age length_of_reg female party_d party_i pct_elections_voted n_comments
outreg2 using output//table4.txt, ///
		label dec(3) 2aster nocons nor2 ///
		ctitle("DV=Neutral or Oppose") append tex(frag)

gen party_cd = "I"
replace party_cd = "D" if party_d==1
replace party_cd = "R" if party_r==1
tab party_cd
		
tab party_cd position_cd, row

// 40B stats

use town_40b_support_comments, clear

total *repeal
disp _b[no_repeal]/(_b[no_repeal]+_b[yes_repeal])

count if housing_support>=.5

// Table A2
use reasons_by_comment, clear
gen n = 1
collapse (sum) n, by(position* reason*)
gsort position -n
by position: gen r = _n
drop position_cd reasons_code
reshape wide reason n, i(r) j(position) string
replace nneutral = nneutral/$nneu
replace noppose = noppose/$nopp
replace nsupport = nsupport/$nsup
keep if r<=10
foreach x in neutral support oppose {
	replace n`x' = 100*n`x'
	tostring n`x', replace format("%10.1f") force
	replace reason`x' = subinstr(reason`x' + " ("+n`x'+"%)", "%", "\%", .)
	
}
listtex reasonsupport reasonneutral reasonoppose using output//tableA2.tex, end("\\") replace

log close
